package sivan.观察者模式;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sivan on 2019/2/28.
 */
public class TeacherSubject implements Subject {

    private List<Observer> observers = new ArrayList<>();
    private String info;

    @Override
    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void deleteObserver(Observer observer) {
        int index = observers.indexOf(observer);
        if (index >= 0){
            observers.remove(index);
        }
    }

    @Override
    public void notifyObserver() {
        System.out.println("TeacherSubject发布新内容：" + this.info);
        observers.forEach(x -> x.update(info));
    }

    public void publishJob(String info){
        this.info = info;
        this.notifyObserver();
    }
}
